unit unit_setup;

{$mode ObjFPC}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  Buttons, StdCtrls, ComCtrls, Spin,
  registry;

type

  { TForm_setup }

  TForm_setup = class(TForm)
    Bevel1: TBevel;
    Bevel14: TBevel;
    Bevel15: TBevel;
    Bevel2: TBevel;
    Bevel3: TBevel;
    BitBtn_2k: TBitBtn;
    BitBtn_set: TBitBtn;
    BitBtn_get: TBitBtn;
    BitBtn_t0: TBitBtn;
    BitBtn_saveauto: TBitBtn;
    BitBtn_save: TBitBtn;
    CheckBox_log: TCheckBox;
    CheckBox_autorun: TCheckBox;
    CheckBox_all: TCheckBox;
    Edit_ip: TEdit;
    Label_ip: TLabel;
    Memo_log: TMemo;
    PageControl1: TPageControl;
    Panel1: TPanel;
    SB_save_ip: TSpeedButton;
    SB_clear: TSpeedButton;
    Shape1: TShape;
    Shape2: TShape;
    Shape3: TShape;
    Shape4: TShape;
    Shape5: TShape;
    Shape6: TShape;
    SpinEdit_left: TSpinEdit;
    SpinEdit_up: TSpinEdit;
    SpinEdit_rigth: TSpinEdit;
    SpinEdit_down: TSpinEdit;
    StaticText1: TStaticText;
    StaticText10: TStaticText;
    StaticText11: TStaticText;
    StaticText2: TStaticText;
    StaticText3: TStaticText;
    StaticText4: TStaticText;
    StaticText5: TStaticText;
    StaticText6: TStaticText;
    StaticText7: TStaticText;
    StaticText8: TStaticText;
    StaticText9: TStaticText;
    TabSheet_debug: TTabSheet;
    TabSheet_log: TTabSheet;
    TabSheet_setup: TTabSheet;
    procedure BitBtn_2kClick(Sender: TObject);
    procedure BitBtn_getClick(Sender: TObject);
    procedure BitBtn_saveautoClick(Sender: TObject);
    procedure BitBtn_saveClick(Sender: TObject);
    procedure BitBtn_setClick(Sender: TObject);
    procedure BitBtn_t0Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure NoShowMyself;
    procedure SB_clearClick(Sender: TObject);
    procedure SB_save_ipClick(Sender: TObject);
    procedure ShowMyself;
  private
    time: TTimeStamp;
  public
    procedure AddLog(s: string; b: boolean = False);

  end;

var
  Form_setup: TForm_setup;

implementation

{$R *.lfm}

uses
  unit_dm;

procedure TForm_setup.NoShowMyself;
begin
  // 不显示详细信息了
  CheckBox_all.Checked := False;
end;

procedure TForm_setup.AddLog(s: string; b: boolean = False);
var
  CurTime: TDateTime;
  t: TTimeStamp;
  i, n: integer;
begin
  if not CheckBox_log.Checked then exit;
  if not b then
  begin
    if not CheckBox_all.Checked then exit;
    if not TabSheet_log.TabVisible then exit;
  end;
  CurTime := now;
  t := DateTimeToTimeStamp(CurTime);
  s := trim(s);
  n := t.Time - time.Time;
  i := 60 - s.Length - IntToStr(n).Length;
  Memo_log.Lines.Add(FormatDateTime('hh:mm:ss:zzz', CurTime) + ' - ' +
    s + Format('%' + IntToStr(i) + 'd', [n]) + ' ms');
  time := t;
end;

procedure TForm_setup.ShowMyself;
var
  Registry: TRegistry;
  CompileCommand: string = '';
begin
  Registry := TRegistry.Create;
  try
    Registry.RootKey := HKEY_LOCAL_MACHINE;
    if Registry.OpenKeyReadOnly('\Software\Microsoft\Windows\CurrentVersion\Run') then
      CompileCommand := Registry.ReadString('laz-capture');
  finally
    Registry.Free;
  end;
  CheckBox_autorun.Checked := CompileCommand <> '';
end;

procedure TForm_setup.BitBtn_saveautoClick(Sender: TObject);
var
  Registry: TRegistry;
begin
  // 是否开机自动运行
  Registry := TRegistry.Create;
  try
    Registry.RootKey := HKEY_LOCAL_MACHINE;
    if Registry.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run', True) then
      if CheckBox_autorun.Checked then
        Registry.WriteString('laz-capture', ParamStr(0) + ' hide')
      else
        Registry.DeleteValue('laz-capture');
  finally
    Registry.Free;
  end;
  ShowMessage('已经保存开机自动运行设置');
end;

procedure TForm_setup.FormCreate(Sender: TObject);
begin
  Edit_ip.Text := DM.S_SYS_SERVER_IP;

  BitBtn_getClick(nil);

  PageControl1.ActivePage := TabSheet_setup;
  // 默认不能调试，需要服务器发送命令开启
  TabSheet_debug.TabVisible := False;

end;


procedure TForm_setup.SB_save_ipClick(Sender: TObject);
begin
  with DM do
  begin
    IniPropStorage1.IniSection := '配置';
    IniPropStorage1.Active := True;
    S_SYS_SERVER_IP := Trim(Edit_ip.Text);
    IniPropStorage1.WriteString('服务器地址', S_SYS_SERVER_IP);
    IniPropStorage1.Active := False;

    DataModuleCreate(nil);
  end;
end;


procedure TForm_setup.BitBtn_saveClick(Sender: TObject);
begin
  with DM do
  begin
    // 保存到ini配置文件
    IniPropStorage1.IniSection := '参数';
    IniPropStorage1.Active := True;
    IniPropStorage1.WriteInteger('left', g_left);
    IniPropStorage1.WriteInteger('up', g_up);
    IniPropStorage1.WriteInteger('down', g_down);
    IniPropStorage1.WriteInteger('rigth', g_rigth);
    IniPropStorage1.Active := False;
  end;
end;

procedure TForm_setup.BitBtn_setClick(Sender: TObject);
begin
  DM.g_left := SpinEdit_left.Value;
  DM.g_up := SpinEdit_up.Value;
  DM.g_down := SpinEdit_down.Value;
  DM.g_rigth := SpinEdit_rigth.Value;
end;

procedure TForm_setup.BitBtn_getClick(Sender: TObject);
begin
  SpinEdit_left.Value := DM.g_left;
  SpinEdit_up.Value := DM.g_up;
  SpinEdit_down.Value := DM.g_down;
  SpinEdit_rigth.Value := DM.g_rigth;
end;

procedure TForm_setup.BitBtn_2kClick(Sender: TObject);
begin
  SpinEdit_left.Value := 1000;
  SpinEdit_up.Value := 1000;
  SpinEdit_down.Value := 1000;
  SpinEdit_rigth.Value := 1000;
end;

procedure TForm_setup.BitBtn_t0Click(Sender: TObject);
begin
  SpinEdit_left.Value := 0;
  SpinEdit_up.Value := 0;
  SpinEdit_down.Value := 0;
  SpinEdit_rigth.Value := 0;
end;

procedure TForm_setup.SB_clearClick(Sender: TObject);
begin
  Memo_log.Lines.Clear;
end;


end.
